package com.javabi.htmlbuilder.html.element.form;

import com.javabi.htmlbuilder.html.HTMLBuilder;
import com.javabi.htmlbuilder.html.HTMLElementList;
import com.javabi.htmlbuilder.html.HTMLElementName;
import com.javabi.htmlbuilder.html.element.content.Text;

/**
 * An HTML Text Input Element
 * <p>
 * &lt;textarea rows="#" cols="#"&gt; ... &lt;/textarea&gt;
 * </p>
 */
public class TextArea extends HTMLElementList<TextArea> {

	/** The name. */
	private String name = null;
	/** The number of rows. */
	private int rows = 0;
	/** The number of columns. */
	private int cols = 0;

	/**
	 * Creates a new text area.
	 */
	public TextArea() {
		add(new Text("")); // Text area always contains something
	}

	/**
	 * Creates a new text area.
	 * @param name the name.
	 */
	public TextArea(String name) {
		this();
		setName(name);
	}

	/**
	 * Creates a new text area.
	 * @param name the name.
	 * @param rows the number of rows.
	 * @param cols the number of columns.
	 */
	public TextArea(String name, int rows, int cols) {
		this();
		setName(name);
		setRows(rows);
		setCols(cols);
	}

	/**
	 * Returns the name
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Returns the rows
	 * @return the rows
	 */
	public final int getRows() {
		return rows;
	}

	/**
	 * Sets the rows
	 * @param rows the rows to set
	 */
	public final void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * Returns the cols
	 * @return the cols
	 */
	public final int getCols() {
		return cols;
	}

	/**
	 * Sets the cols
	 * @param cols the cols to set
	 */
	public final void setCols(int cols) {
		this.cols = cols;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.TEXT_AREA;
	}

	/**
	 * Append attributes to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendAttributesTo(HTMLBuilder html) {
		if (name != null) {
			html.attribute("name", name);
		}
		if (rows > 0) {
			html.attribute("rows", rows);
		}
		if (cols > 0) {
			html.attribute("cols", cols);
		}
		return html;
	}

}
